package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class IEmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;



    @Override
    public void save(Employee employee) {
        //给员工信息设置盐值
        employee.setSalt(StrUtils.getRandomString(4));
        //员工密码加密
        employee.setPassword(MD5Utils.encrypByMd5(employee.getPassword()+employee.getSalt()));
        //将后台人员信息赋值到登录信息表中
        LoginInfo loginInfo=employee2LoginInfo(employee);
        //保存登录信息到t_LonginInfo表格中
        loginInfoMapper.save(loginInfo);
        //将loginInfo中的信息保存到employee中
        employee.setLoginInfo(loginInfo);
        //保存后台人员信息到数据库中t_employeee
        super.save(employee);
    }

    private LoginInfo employee2LoginInfo(Employee employee) {
        LoginInfo loginInfo = new LoginInfo();
        //将后台人员中相同的信息赋值到登录信息对象中
        BeanUtils.copyProperties(employee, loginInfo);
        //将类型设置为后台人员
        loginInfo.setType(false);
        return loginInfo;
    }

    @Override
    public Employee findByLoginInfoId(Long id) {
        return employeeMapper.findByLoginInfoId(id);
    }
}
